<template>
  <div class="params">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 警告框 -->
      <el-alert title="注意：只允许第三级分类设置相关参数" type="warning" show-icon :closable="false"></el-alert>
      <!-- 选择商品分类区域 -->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类：</span>
          <el-cascader
            v-model="selectedCateKeys"
            :options="cateList"
            :props="{ expandTrigger: 'hover' ,value:'cat_id',label:'cat_name',children:'children'}"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <!-- 添加动态参数的面板 -->
        <el-tab-pane label="动态参数" name="many">
          <el-button
            type="primary"
            size="small"
            :disabled="isBtnDisabled"
            @click="addDialogVisible=true"
          >添加参数</el-button>
          <!-- 动态参数表格 -->
          <el-table stripe border :data="manyTableData">
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index" label="序号"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog">编辑</el-button>
                <el-button type="primary" icon="el-icon-search" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <!-- 添加静态属性的面板 -->
        <el-tab-pane label="静态属性" name="only">
          <el-button
            type="primary"
            size="small"
            :disabled="isBtnDisabled"
            @click="addDialogVisible=true"
          >添加属性</el-button>
          <!-- 静态属性表格 -->
          <el-table stripe border :data="onlyTableData">
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index" label="序号"></el-table-column>
            <el-table-column label="属性名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog">编辑</el-button>
                <el-button type="primary" icon="el-icon-search" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加参数的对话框 -->
    <el-dialog
      :title="'添加'+titleText"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClose"
    >
      <el-form :rules="addFormRules" ref="addFormRef" :model="addForm" label-width="80px">
        <el-form-item :label="titleText" prop="attr_name">
          <el-input v-model="addForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addParams">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改参数的对话框 -->
    <el-dialog
      :title="'修改'+titleText"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClose"
    >
      <el-form :rules="editFormRules" ref="editFormRef" :model="editForm" label-width="80px">
        <el-form-item :label="titleText" prop="attr_name">
          <el-input v-model="editForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editParams">确 定</el-button>
      </span>
    </el-dialog>
  </div>
  <!-------------------------------------------------------------------------------------------------------------------------->
  <!-------------------------------------------------------------------------------------------------------------------------->
</template>

<script>
export default {
  name: "params",
  data() {
    return {
      /**
       * 商品分类列表
       */
      cateList: [],
      /**
       * 选中的商品id
       */
      selectedCateKeys: [],
      /**
       * 被激活的页签
       */
      activeName: "many",
      /**
       * 动态参数表格数据
       */
      manyTableData: [],
      /**
       * 静态属性表格参数
       */
      onlyTableData: [],
      /**
       * 控制添加对话框的显示与隐藏
       */
      addDialogVisible: false,

      addForm:{
        attr_name:''
      },

      addFormRules:{
        attr_name:[
          {required: true, message:'请输入参数名称', trigger: 'blur'}
        ]
      },
      editDialogVisible:false,
      editForm:{
        attr_name:''
      },
      editFormRules:{
        attr_name:[
          {required: true, message:'请输入参数名称', trigger: 'blur'}
        ]
      },
      editParams:{}
    };
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  },
  computed: {
    isBtnDisabled() {
      return this.selectedCateKeys.length !== 3 ? true : false;
    },
    cateId() {
      // this.selectedCateKeys.length!==3?this.selectedCateKeys[2]:null;
      if (this.selectedCateKeys.length === 3) {
        return this.selectedCateKeys[2];
      } else {
        return null;
      }
    },
    titleText() {
      if (this.activeName === "many") {
        return "动态参数";
      } else {
        return "静态属性";
      }
    }
  },
  methods: {
    /**
     * 获取商品分类列表
     */
    async getCateList() {
      const { data: res } = await this.$http.get("categories");
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类失败");
      }
      this.cateList = res.data;
      // console.log(this.cateList);
    },
    /**
     * 级联选择框发生变化
     */
    handleChange() {
      // console.log(this.selectedCateKeys)
      this.getParamsData();
    },

    /**
     * 页签点击处理函数
     */
    handleTabClick() {
      this.getParamsData();
    },
    /**
     * 获取参数数据
     */
    async getParamsData() {
      if (this.selectedCateKeys.length !== 3) {
        this.selectedCateKeys = [];
        return this.$message.warning("只可选中三级分类商品");
      }
      const { data: res } = await this.$http.get(
        `categories/${this.cateId}/attributes`,
        {
          params: {
            sel: this.activeName
          }
        }
      );
      if (res.meta.status !== 200) {
        return this.$message.error("获取参数列表失败");
      }
      // console.log(res.data);
      if (this.activeName === "many") {
        this.manyTableData = res.data;
      } else {
        this.onlyTableData = res.data;
      }
    },

    /**
     * 添加参数的表单关闭
     */
    addDialogClose(){
      this.$refs.addFormRef.resetFields()
    },

    /**
     * 确认添加参数
     */
    addParams(){
      this.$refs.addFormRef.validate(async valid=>{
        if(!valid){
          return this.$message.error('参数校验失败')
        }else{
          const {data:res} = await this.$http.post(`categories/${this.cateId}/attributes`,{
            attr_name:this.addForm.attr_name,
            attr_sel:this.activeName
          })
          if(res.meta.status!==201){
            this.$message.error('添加参数失误')
          }
          this.$message.success('添加参数成功')
          this.getParamsData()
          this.addDialogVisible = false
        }
      })
    },
    editDialogClose(){
      this.editDialogVisible = false
    },
    showEditDialog(){
      this.editDialogVisible = true
    }
  },
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  created() {
    this.getCateList();
  }
};
</script>

<style lang="less" scoped>
.params {
  .cat_opt {
    margin: 15px 0px;
  }
}
</style>